﻿Class Application

    ' Application-level events, such as Startup, Exit, and DispatcherUnhandledException
    ' can be handled in this file.
    Public Shared _MainViewModel As MainModel = New MainModel

    Private WithEvents _notificationIcon As System.Windows.Forms.NotifyIcon = New System.Windows.Forms.NotifyIcon
    Public Property NotificationIcon() As System.Windows.Forms.NotifyIcon
        Get
            Return _notificationIcon
        End Get
        Set(ByVal value As System.Windows.Forms.NotifyIcon)
            _notificationIcon = value
        End Set
    End Property




    Private Sub App_Activated(ByVal sender As Object, ByVal e As EventArgs)
        ' Application activated

        For Each _wnd In Application.Current.Windows
            If TryCast(_wnd, Window).Tag = "InfoWindow" Then TryCast(_wnd, Window).Show()
        Next

    End Sub

    Private Sub App_Deactivated(ByVal sender As Object, ByVal e As EventArgs)
        ' Application deactivated
        
        For Each _wnd In Application.Current.Windows
            If TryCast(_wnd, Window).IsVisible And TryCast(_wnd, Window).Tag = "InfoWindow" Then TryCast(_wnd, Window).Hide()
        Next
        

    End Sub

    Private Sub Application_Exit(sender As Object, e As ExitEventArgs) Handles Me.Exit

        NotificationIcon.Visible = False

    End Sub

    Private Sub Application_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup

        If Not ginkyo.AdvancedApi.Debug.RestartWithUACElevation.IsAdmin Then ginkyo.AdvancedApi.Debug.RestartWithUACElevation.RestartWithUACElevation()

        NotificationIcon.Text = "Tunlr.net DNS"
        NotificationIcon.Icon = New System.Drawing.Icon(Application.GetResourceStream(New Uri("pack://application:,,,/Tunlr;Component/Glimpse Icon x16.ico")).Stream)
        NotificationIcon.Visible = True

    End Sub



    Private Sub _notificationIcon_MouseClick(sender As Object, e As Forms.MouseEventArgs) Handles _notificationIcon.MouseClick

        Select Case e.Button
            Case Forms.MouseButtons.Left

                For Each _wnd In Application.Current.Windows
                    If TryCast(_wnd, Window).Tag = "InfoWindow" Then
                        TryCast(_wnd, Window).Topmost = True
                        TryCast(_wnd, Window).Show()
                        TryCast(_wnd, Window).Focus()
                        TryCast(_wnd, Window).Topmost = False

                    End If

                Next


            Case Forms.MouseButtons.Right
                Dim _ToolbarContxt = Application.Current.FindResource("TrayIconContextMenu")
                _ToolbarContxt.DataContext = _MainViewModel
                _ToolbarContxt.Placement = Primitives.PlacementMode.MousePoint
                _ToolbarContxt.IsOpen = True


        End Select



    End Sub


End Class
